import numpy as np
from model.utils import acc_dist, finish_prob
import heapq
from utils2 import get_topk, get_dict_fmt
import json


# order、driver文件
order_file = r'../data/order_20161101_120s.csv'
drive_file = r'../data/driver_20161101_120s.csv'

# 打开order、driver文件
with open(order_file) as f:
    orders = np.loadtxt(f, str, delimiter = ",")

with open(drive_file) as f:
    drivers = np.loadtxt(f, str, delimiter = ",")

# # 一对多的情况
# order = orders[0] # 技能属性为3，表示需要两个技能分别为1和2
# # 先找到技能一的top11
# drivers_topk_1 = utils2.get_topk('1', order, drivers)
# for driver in drivers_topk_1:
#     print(driver)
# print("+++++++++++++++++++++++++++++++++++++")
# # 先找到技能二的top11
# drivers_topk_2 = utils2.get_topk('2', order, drivers)
# for driver in drivers_topk_2:
#     print(driver)
# # 计算订单完成率
# order_lng, order_lat = float(order[3]), float(order[4])
# driver = drivers_topk_2[0]
# driver_lng, driver_lat = float(driver[2]), float(driver[3])
# dist = acc_dist(order_lng, order_lat, driver_lng, driver_lat)
# order_start_loc = []
# order_start_loc.append(float(order[3]))
# order_start_loc.append(float(order[4]))
# order_finish_loc = []
# order_finish_loc.append(float(driver[2]))
# order_finish_loc.append(float(driver[3]))
# order_start_time = order[1]
# done_prob = finish_prob(dist, order_start_loc, order_finish_loc, order_start_time)
# print(done_prob)

# 一对一的情况
list = []
order_count = 0
for order in orders:
    if order[8] == str(3):
        continue

    top11_drivers = get_topk(order[8], order, drivers)
    list_to_json = get_dict_fmt(order, top11_drivers)
    list.extend(list_to_json)

    order_count += 1

print(order_count)

# 一对多的情况
list_double = []
order_double_count = 0
for order in orders:
    if order[8] == str(1) or order[8] == str(2):
        continue

    # 匹配技能一
    top11_drivers_skill1 = get_topk(str(1), order, drivers)
    list_to_json_skill1 = get_dict_fmt(order, top11_drivers_skill1)
    list_double.extend(list_to_json_skill1)

    # 匹配技能二
    order_id = ~int(order[0])
    order[0] = str(order_id)
    top11_drivers_skill2 = get_topk(str(2), order, drivers)
    list_to_json_skill2 = get_dict_fmt(order, top11_drivers_skill2)
    list_double.extend(list_to_json_skill2)

    order_double_count += 1

print(order_double_count)

# list.extend(list_double)

# json_str = json.dumps(list1)
fp = open('dispatch_observ', 'w')
json.dump(list, fp)

